using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace zeroflag.Windows.Custom
{
	public partial class CustomView<ControllerType> :
		ACustomView, ICustomView<ControllerType>
		where ControllerType :
		ACustomController,
		new()
	{
		protected CustomView()
			: this(new ControllerType())
		{
		}

		public CustomView(ControllerType controller)
		{
			this.Controller = controller;
			InitializeComponent();
		}

		public override RenderTarget RenderTarget
		{
			get { return renderTarget; }
			set
			{
				if (renderTarget != value)
				{
					if (renderTarget != null)
					{
						renderTarget.Paint -= this.renderTarget_Paint;
					}
					//this.RenderGraphics = null;
					renderTarget = value;
					//value.Paint += this.renderTarget_Paint;
					//this.RenderGraphics = value.CreateGraphics();
					if (renderTarget != null)
					{
						renderTarget.Paint += this.renderTarget_Paint;
					}
				}
			}
		}


		protected internal override ACustomController GetController()
		{
			return (ACustomController)this.Controller;
		}

		protected internal override void SetController(ACustomController value)
		{
			this.Controller = (ControllerType)value;
		}

		ControllerType m_Controller;
		[System.ComponentModel.Category("Tokens")]
		public virtual ControllerType Controller
		{
			get
			{
				//return this.GetController();
				return m_Controller;
			}
			set
			{
				//this.SetController((ControllerType)value);
				if (m_Controller != value)
				{
					if (m_Controller != null)
					{
						m_Controller.SetView(null);
						this.components.Remove(m_Controller);
					}
					m_Controller = value;
					if (m_Controller != null)
					{
						m_Controller.SetView(this);
						this.components.Add(m_Controller);
					}
				}
			}
		}

		//Graphics renderGraphics;

		//public Graphics RenderGraphics
		//{
		//    get
		//    {
		//        if (renderGraphics == null)
		//        {
		//            this.renderGraphics = this.RenderTarget.CreateGraphics();
		//        }
		//        return renderGraphics;
		//    }
		//    set { renderGraphics = value; }
		//}
		private void renderTarget_Paint(object sender, PaintEventArgs e)
		{
			this.Render(e.Graphics);
			//this.Render(this.RenderGraphics);
		}

		protected virtual void Render(Graphics g)
		{
			g.Clear(this.BackColor);

			if (this.Controller != null)
			{
				this.RenderTarget.SuspendLayout();
				RenderArea area = new RenderArea(g, this.RenderTarget);
				this.Controller.Render(area);
				area.Apply();
				g.DrawRectangle(Pens.Black, this.RenderTarget.ClientRectangle);
				this.RenderTarget.ResumeLayout();
			}
			else
				g.DrawString("No Controller!", this.Font, new SolidBrush(this.ForeColor), this.ClientRectangle.Location);

		}


		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);

			this.RenderTarget.Render();

			//this.Render(this.RenderGraphics);
			//this.RenderTarget.Render();
		}

	}
}
